package com.woniuxy.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.store.infra.mapper.PackageFunctionMapper;
import com.woniuxy.store.infra.model.Function;
import com.woniuxy.store.infra.model.Package;
import com.woniuxy.store.infra.model.PackageFunction;
import com.woniuxy.store.service.FunctionService;
import com.woniuxy.store.service.PackageFunctionService;
import com.woniuxy.store.service.PackageService;
import exception.MallException;
import org.springframework.stereotype.Service;
import result.CommonResultCodeEnum;
import utils.Assert;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zz
 * @since 2023-07-20
 */
@Service
public class PackageFunctionServiceImpl extends ServiceImpl<PackageFunctionMapper, PackageFunction> implements PackageFunctionService {

    @Resource
    private PackageService packageService;
    @Resource
    private FunctionService functionService;

    @Override
    public void addPackFun(PackageFunction packageFunction) {
        Package pack = packageService.getById(packageFunction.getPackageId());
        Function function = functionService.getById(packageFunction.getFunctionId());
        if (pack==null||function==null){
            throw new MallException(CommonResultCodeEnum.INVALID_PARAM);
        }
        QueryWrapper<PackageFunction> wrapper = new QueryWrapper<>();
        wrapper.eq("package_id",packageFunction.getPackageId());
        wrapper.eq("function_id",packageFunction.getFunctionId());
        Assert.isNull(getOne(wrapper),"该套餐已存在");
        save(packageFunction);
    }
}
